<template>
	<view class="container">
		<view class="user-info_card z-white-font u-padding-40 u-margin-bottom-20 white-bg z-b-r z-bg-color">
			<view v-if="!haveUserInfoShow">您还没有添加个人信息，点击下方按钮添加</view>
			<view v-if="haveUserInfoShow">
				<view class="z-font-lg">姓名：{{ userData.name }}({{ userData.userType === 'wk' ? '员工' : '项目经理' }})</view>
				<view class="z-font-lg">编号：{{ userData.wkID }}</view>
				<view class="z-font-lg">电话：{{ userData.phone }}</view>
				<view class="z-font-lg">地址：{{ userData.address }} {{ userData.detailAddre }}</view>
			</view>
		</view>
		<view class="add-user-info u-padding-40 white-bg z-b-r u-flex" @click="editUserInfo" hover-class="h-class">
			<view class="u-margin-right-10"><u-icon name="edit-pen"></u-icon></view>
			<view class="">{{haveUserInfoShow ? '编辑个人信息' : '添加个人信息'}}</view>
		</view>
		
		<!-- 修改信息的时候密码确认 -->
		<u-popup v-model="popupShow" width="80%" negative-top="200" mode="center" closeable="true" border-radius="20">
			<view class="check-pass-box u-padding-40">
				<view class="u-margin-bottom-20">请输入在个人信息中设置的密码</view>
				<u-input v-model="password" type="password" :password-icon="true" placeholder="请输入密码" border="true"/>
				<view class="u-margin-top-20">
					<u-button class="z-button" type="primary" @click="checkPass">验证密码</u-button>
				</view>
			</view>
		</u-popup>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {mapState} from "vuex";
	import {encrypt} from "@/common/encrypt.js";
	export default {
		data() {
			return {
				userData:{},
				haveUserInfoShow:false,
				popupShow:false,
				password:""
			}
		},
		computed:{
			...mapState({
				userID:"userID",
				token:"token",
				wkID:"wkID"
			})
		},
		async onLoad() {
			// 获取用户数据（如果存在）
			if(this.wkID == ""){
				return false;
			}
			this.$loading("请稍后")
			let res = await this.$network({
				api:"main",
				data:{
					action:"user/getUserInfo",
					data:{
						_userID:this.userID,
						token:this.token
					}
				}
			})
			uni.hideLoading();
			if(res.status === 0){
				this.userData = res.data[0];
				this.haveUserInfoShow = true;
			}
		},
		methods: {
			//修改按钮
			editUserInfo(){
				if(this.haveUserInfoShow){
					this.popupShow = true;
				} else {
					uni.navigateTo({url:`../user-info/user-info?haveUserInfoShow=${this.haveUserInfoShow}`})
				}
			},
			// 验证密码
			checkPass(){
				let encodePass = encrypt.encodeHex(this.password);
				if(encodePass == this.userData.password){
					this.popupShow = false;
					this.password = ""
					uni.navigateTo({url:`../user-info/user-info?haveUserInfoShow=${this.haveUserInfoShow}`})
				} else {
					this.$refs.uToast.show({
						title: '密码错误，请确认后重试',
						type: 'error'
					})
				}
			}
		}
	}
</script>
